package Modelo;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
	
	private static Mail instancia;
	
	public Mail getInstancia(){
		if (instancia == null){
			Mail instancia = new Mail();
		};
		return instancia;
	}

	public Mail() {	
		super();				
	}
	
	public static void enviarMail(String to, String subject,String body) {
		
try {
			
			//PROPIEDADES DE LA CONEXION
			Properties prop = new Properties();
			prop.setProperty("mail.smtp.host", "smtp.gmail.com");
			prop.setProperty("mail.smtp.starttls.enable", "true");
			prop.setProperty("mail.smtp.port", "587");
			prop.setProperty("mail.smtp.user", "elopeziturriche@gmail.com");
			prop.setProperty("mail.smtp.auth", "true");
			
			//PREPARAMOS LA SESION
			
			Session session = Session.getInstance(prop);
			
			//CONSTRUIMOS EL MENSAJE
			
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress("elopeziturriche@gmail.com"));
			message.addRecipient(
					Message.RecipientType.TO,
					new InternetAddress(to));
			message.setSubject(subject);
			message.setText(body);
			
			//LO ENVIAMOS
			Transport t = session.getTransport("smtp");
			t.connect("telefoniapoo@gmail.com","telefonia1");
			t.sendMessage(message, message.getAllRecipients());
			
			//CIERRE
			t.close();
			
		}	catch(Exception e) {
			
			System.out.println(e.getMessage());
			
		}
	}

}
